package com.oa.web.action;

import com.oa.service.EmpService;
import com.oa.utils.FileUtils;
import com.oa.web.action.base.BaseAction;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@Scope("prototype")
public class UploadAction {

    @Autowired
    private EmpService empService;

    //员工头像上传
    private File file;
    private String fileFileName;

    //重新上传时删除上次的图片
    private String lastpath;

    //上传图片有待完善
    public String uploadfile() {
        String root = ServletActionContext.getServletContext().getRealPath("/images/empimgs");

        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
        String dateString = formatter.format(currentTime);

        String fn = fileFileName;

        if (file != null) {
            String filename = dateString + fn.substring(fn.lastIndexOf("."), fn.length());

            File savefile = new File(root, filename);

            //写入文件
            FileUtils.writeFile(file, savefile);

            JSONObject jobj = new JSONObject();
            jobj.put("src", "images/empimgs/" + savefile.getName());
            jobj.put("path", savefile.getAbsolutePath());

            Map<String, Object> map = new HashMap<>();

            map.put("code", 0);
            map.put("msg", "");
            map.put("data", jobj);

            JSONObject jsonObject = JSONObject.fromObject(map);

            this.write2Json(jsonObject.toString());
        }

        return "none";
    }

    //重新上传时删除上次的图片
    public String deleteFile() {
        System.out.println(lastpath);
        if (StringUtils.isNotBlank(lastpath)) {
            FileUtils.deleteFile(lastpath);
        }
        return "none";
    }


    //暂时不做
    public void deleteImg() {
        String root = ServletActionContext.getServletContext().getRealPath("");
//        System.out.println(root); C:\Users\Administrator\Desktop\Daily\src\main\webapp

        if (StringUtils.isNotBlank(lastpath)) {

            //如果数据库还有其他员工使用此图片，将不删除此图片
            boolean is = empService.canDelImag(lastpath);

            if (is) {

                String path = root + "\\" + lastpath.replace('/', '\\');
                ;

                System.out.println(path);

                File lafile = new File(path);

                if (lafile.exists()) {
//                lafile.delete();
                    System.out.println("exists");
                }

            } else {
                System.out.println("++++++");
            }
        }
    }

    public void write2Json(String json) {
        String contentType = "application/json;charset=utf-8";
        write(contentType, json);
    }

    public void write(String contentType, String textORjson) {
        HttpServletResponse response = ServletActionContext.getResponse();

        response.setContentType(contentType);

        PrintWriter out = null;
        try {
            out = response.getWriter();
            out.write(textORjson);
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }


    public void setFile(File file) {
        this.file = file;
    }

    public void setFileFileName(String fileFileName) {
        this.fileFileName = fileFileName;
    }

    public void setLastpath(String lastpath) {
        this.lastpath = lastpath;
    }
}
